﻿using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {
        [CtxMenuItem("Transform/Child HideFlags/HideInHierarchy",
            "Child objects will not appear in the hierarchy.",
            ContextRequirements.Children, 0)]
        public static void TransformHideFlagHideInHierarchy() { TransformSetFlagsOnChildern(HideFlags.HideInHierarchy); }

        [CtxMenuItem("Transform/Child HideFlags/HideInInspector",
            "Child objects will not appear in the inspector.",
            ContextRequirements.Children, 0)]
        public static void TransformHideFlagHideInInspector() { TransformSetFlagsOnChildern(HideFlags.HideInInspector); }

        [CtxMenuItem("Transform/Child HideFlags/None",
            "Child objects will appear normally.",
            ContextRequirements.Children, 0)]
        public static void TransformHideFlagNone() { TransformSetFlagsOnChildern(HideFlags.None); }

        [CtxMenuItem("Transform/Child HideFlags/NotEditable",
            "Child objects will not be editable.",
            ContextRequirements.Children, 0)]
        public static void TransformHideFlagNotEditable() { TransformSetFlagsOnChildern(HideFlags.NotEditable); }

        public static void TransformSetFlagsOnChildern(HideFlags flags)
        {
            const string undoLabel = "Set HideFlags";
            foreach (var item in SkinnContext.GetContextItems())
            {
                var context = item.GetComponent<Transform>();
                if (SkinnEx.IsNullOrNotInAScene(context)) return;
                var transforms = context.transform.GetAllChildren(false).ToArray();
                for (int i = 0; i < transforms.Length; i++)
                {
                    Undo.RecordObject(transforms[i].gameObject, undoLabel);
                    transforms[i].gameObject.hideFlags = flags;
                }
            }
            SkinnContext.Finished();
        }
    }
}